package algorithms;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class Record {
	private String str;
	private BufferedWriter inFile;
	private FileInputStream fromFile;
	private boolean isEncoded = false;

	public Record(File file, boolean toEncode) throws IOException {
		if (toEncode) {
			inFile = new BufferedWriter(new FileWriter(file));
		} else
			fromFile = new FileInputStream(file);
		str = "";
	}

	public void add(String toWrite, boolean isLast) throws IOException {
		str += toWrite;
		if (str.isEmpty()) {
			inFile.close();
			return;
		}
		boolean withChar = false;

		for (int i = 0; i < toWrite.length(); i++)
			if (!(toWrite.charAt(i) == '0' | toWrite.charAt(i) == '1')) {
				withChar = true;
				break;
			}
		if (withChar) {
			if (isEncoded) {
				isEncoded = false;
				inFile.append((char) 1D);
				inFile.append((char) 7);
				inFile.append((char) 1D);
			}
			inFile.append(str);
			str = "";
			return;
		} else {
			if (str.length() < 14 & !isLast)
				return;
			if (!isEncoded) {
				isEncoded = true;
				inFile.append((char) 1D);
				inFile.append((char) 7);
				inFile.append((char) 1D);
			}
			while (str.length() > 7) {
				int inDecimal = 0;
				for (int i = 0; i < 7; i++) {
					if (str.charAt(i) == '1')
						inDecimal += Math.pow(2, (6 - i));
				}
				inFile.append((char) inDecimal);
				str = str.substring(7);
			}
			if (isLast) {
				isEncoded = false;
				inFile.append((char) 1D);
				inFile.append((char) 7);
				inFile.append((char) 1D);

				inFile.append(str);
				str = "";
				inFile.close();
			}
			return;
		}
	}

	public void decodeToHuffman(File file) throws IOException {
		str = "";

		// Generate tmp file name
		String tmpFileName = file.getAbsolutePath();
		while (tmpFileName.charAt(tmpFileName.length() - 1) != '.')
			tmpFileName = tmpFileName.substring(0, tmpFileName.length() - 1);
		tmpFileName += "tmp";
		FileOutputStream outputFile = new FileOutputStream(
				new File(tmpFileName));

		char t2 = (char) fromFile.read();
		char t3 = (char) fromFile.read();
		char t = t2;
		t2 = t3;
		t3 = (char) fromFile.read();
		while (t != (char) -1) {
			if (!(t == 1D & t2 == 7 & t3 == 1D))
				outputFile.write(t);
			else {
				t = (char) fromFile.read();
				t2 = (char) fromFile.read();
				t3 = (char) fromFile.read();
				while (!((t == 1D) & (t2 == 7) & (t3 == 1D)) & t != (char) -1) {
					int decimal = t;
					String binary = "";
					for (int i = 0; i < 7; i++) {
						if (decimal % 2 == 0)
							binary = '0' + binary;
						else
							binary = '1' + binary;
						decimal /= 2;
					}
					decimal = 0;
					outputFile.write(binary.getBytes());
					t = t2;
					t2 = t3;
					t3 = (char) fromFile.read();
				}
				t3 = (char) fromFile.read();
				t2 = t3;
				t3 = (char) fromFile.read();
			}
			if (t != (char) -1)
				t = t2;
			t2 = t3;
			t3 = (char) fromFile.read();
		}
		outputFile.close();
		fromFile.close();
	}

}
